package com.jushu.storbox.view.dialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.util.NameUtils;
import com.jushu.storbox.util.PublicTools;
import com.huawei.sharedrive.sdk.android.model.request.FolderCreateRequest;

public class CreateFolderDialog {

	public void createFolder(final Activity activity, final String ownerId, final String folderId, final Handler msgHandler
			,final ProgressDialog loading_ll){

        final ClouddriveDialog createFolder = new ClouddriveDialog(activity, R.style.dialog_upload,
            R.layout.window_dialog_create_folder);
        WindowManager windowManager=activity.getWindowManager();
        Display display=windowManager.getDefaultDisplay();
        createFolder.setWidth(display.getWidth());
        createFolder.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
        View conventView = createFolder.getConventView();
        TextView confirm_btn = (TextView) conventView.findViewById(R.id.confirm_btn);
        TextView cancle_btn = (TextView) conventView.findViewById(R.id.cancle_btn);
        TextView tittleText = (TextView) conventView.findViewById(R.id.tittle_tv);
        final EditText input_et = (EditText) conventView.findViewById(R.id.inputText_et);
        tittleText.setText(activity.getString(R.string.create_folder));
        
        cancle_btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                createFolder.dismiss();
                if (loading_ll.isShowing())
                {
                	loading_ll.dismiss();
                }
            }
        });
        confirm_btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!ShareDriveApplication.getInstance().isFastDoubleClick())
                {
                    if (ShareDriveApplication.getInstance()
    						.getWifiController().getNetworkState())
                    {
                        if (!loading_ll.isShowing())
                        {
                        	loading_ll.show();
                        }
                    }
                    createFolder.dismiss();
                    if (!ShareDriveApplication.getInstance()
    						.getWifiController().getNetworkState())
                    {
                        Toast.makeText(activity,
                        		activity.getString(R.string.allfile_search_is_not_vailible),
                            Toast.LENGTH_SHORT).show();
                        
                    }
                    else
                    {
                        if (!NameUtils.isValidFileName(input_et.getText().toString()))
                        {
                            Toast.makeText(activity,
                                activity.getString(R.string.allfile_file_name_not_standard),
                                Toast.LENGTH_SHORT).show();
                            if (loading_ll.isShowing())
                            {
                            	loading_ll.dismiss();
                            }
                            createFolder(activity,ownerId,folderId,msgHandler,loading_ll);
                            return;
                        }
                        else
                        {
                            final FolderCreateRequest request = new FolderCreateRequest();
                            request.setName(input_et.getText().toString());
                            request.setParent(folderId);
                            new FolderAction().createFolder(ownerId, request, activity, msgHandler);
                        }
                    }
                }
            }
        });
        createFolder.show();
        PublicTools.showKey(input_et, activity);
    
	}
}
